package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RequestMapping("/admin/product")
@RestController
public class SkuController {


    @Autowired
    SkuInfoService skuInfoService;

    /**
     * SkuInfo分页查询
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuPages(@PathVariable("page")Long page,
                              @PathVariable("limit")Long limit){

        Page<SkuInfo> pageObj = new Page<>(page,limit);
        Page<SkuInfo> infoPage = skuInfoService.page(pageObj);
        return Result.ok(infoPage);
    }

    /**
     * 保存skuInfo信息
     * @param skuInfo
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){

        skuInfoService.saveSkuInfo(skuInfo);
        return Result.ok();
    }


    /**
     * 上架商品
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSaleSku(@PathVariable("skuId")Long skuId){
        skuInfoService.updateSkuSaleStatus(skuId,1);
        return Result.ok();
    }

    /**
     * 下架商品
     * @param skuId
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSaleSku(@PathVariable("skuId")Long skuId){
        skuInfoService.updateSkuSaleStatus(skuId,0);
        return Result.ok();
    }
}
